﻿using SmartParking.Client.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace SmartParking.Client.Start.ViewModels
{
    public class LoginViewModel:BindableBase
    {
        public LoginViewModel(ILoginBLL loginBLL)
        {
            this._loginBLL = loginBLL;
        }
        #region 
        private string _userName ="admin";

		public string UserName
		{
			get { return _userName; }
			set { SetProperty<string>(ref _userName, value); }
		}

        private string _password = "123456";

        public string Password
        {
            get { return _password; }
            set { SetProperty<string>(ref _password, value); }
        }

        private string _errorMsg;
        private readonly ILoginBLL _loginBLL;

        public string ErrorMsg
        {
            get { return _errorMsg; }
            set { SetProperty<string>(ref _errorMsg, value); }
        }

        #endregion
        // 登录命令
        public ICommand LoginCommand {
            get => new DelegateCommand<Object>(OnLoad);
        }

        private void OnLoad(object obj)
        {
            try {
                ErrorMsg = "";
                if (string.IsNullOrEmpty(UserName)) {
                    ErrorMsg = "请输入用户名";
                    return;
                }
                if (string.IsNullOrEmpty(Password))
                {
                    ErrorMsg = "请输入密码";
                    return;
                }
                // 登录操作
                var res = _loginBLL.Login(UserName, Password).GetAwaiter().GetResult();
                if (res) {
                    (obj as Window).DialogResult = true;
                } else {
                    ErrorMsg = "登录失败！";
                }
            } catch (Exception ex) {
                ErrorMsg = "登录失败！"+ex.Message;
            }
        }
    }
}
